#include "./analog.h"
#include "../device/dma.h"
#include "../device/gpio.h"
#include "../device/adc.h"
#include "../utils/alg.h"

namespace driver {

using namespace device::dma;
using namespace device::gpio;
namespace adc = device::adc;
using namespace utils;

using ADC_DMA = Dma<DMA1_BASE, 1>;
using ADC_DMA_CHAN = DmaChan<DMA1_Channel1_BASE, 0, 1>;

using AIN = PC<2>; // CHAN12

Analog::Analog() {
    RCC->APB2ENR |= RCC_APB2ENR_IOPCEN | RCC_APB2ENR_ADC1EN;
    
    config_analog_input<AIN>();
    adc::init<ADC1_BASE, ADC_DMA, ADC_DMA_CHAN, 12>(m_values);
}

uint16_t Analog::change_value() {
    uint16_t cur = value();
    if (alg::abs(m_last, cur) > 3) {
        m_last = cur;
    }
    return m_last;
}

}
